<template>
  <t-dialog
    header="选择用户"
    :visible.sync="visible"
    :onClose="closeDialog"
    :closeOnOverlayClick="false"
    @confirm="addSubmit"
    width="1000px"
    top="5%"
  >
    <t-form ref="form" :colon="true"  :data="queryParams" label-width="80" @submit="()=>{this.pagination.current=1;this.queryParams.pageNum=1;this.getList()}" @reset="()=>{this.pagination.current=1;this.queryParams.pageNum=1;this.getList()}">
      <t-row style="border-bottom: 1px solid #E5E6EB;margin-bottom: 10px">
        <t-col :span="11" class="query-form-inline">
          <t-form-item label="登录名称" name="userName">
            <t-input
              v-model="queryParams.userName"
              clearable
              placeholder="请输入登录名称"
              style="width: 240px"
            />
          </t-form-item>
          <t-form-item label="手机号码" name="phonenumber">
            <t-input
              v-model="queryParams.phonenumber"
              clearable
              placeholder="请输入手机号码"
              style="width: 240px"
            />
          </t-form-item>
        </t-col>
        <t-col :span="1" class="operation-container">
          <t-button theme="primary" type="submit"> 查询</t-button>
          <t-button theme="default" variant="outline" type="reset">重置</t-button>
        </t-col>
      </t-row>
    </t-form>
    <t-table
      rowKey="id"
      :data="data"
      :columns="columns"
      @select-change="rehandleSelectChange"
      hover
      :pagination="pagination"
      :loading="dataLoading"
      :selected-row-keys.sync="userIds"
    >
      <template #status="{ row }">
        <t-tag v-if="row.status === '0'" theme="success" variant="light">正常</t-tag>
        <t-tag v-else theme="danger" variant="light">停用</t-tag>
      </template>
      <template #op="{  }">
        <t-button @click="cancelUserRole('111')" size="small" variant="text" class="t-button-link">
          <icon name="close-circle" slot="icon"/>
          取消授权
        </t-button>
      </template>
    </t-table>
  </t-dialog>
</template>

<script>
import { unallocatedUserList, authUserSelectAll } from "@/api/system/role";

export default {
  name: "add-role-user",
  props:["roleId"],
  data() {
    return {
      userIds:[],
      visible: true,
      queryParams: {
        userName: "",
        phonenumber: "",
      },
      data:[],
      columns:[
        {
          colKey: 'row-select',
          type: 'multiple',
          width:55,
        },
        {
          colKey: "userName",
          title: "登录名称",
          align: "center"
        },
        {
          colKey: "nickName",
          title: "真实姓名",
          align: "center"
        },
        {
          colKey: "email",
          title: "邮箱",
          align: "center"
        },
        {
          colKey: "phonenumber",
          title: "手机",
          align: "center"
        },
        {
          colKey: "status",
          title: "状态",
          align: "center"
        },
        {
          colKey: "createTime",
          title: "创建时间",
          align: "center"
        },
      ],
      dataLoading: false,
      pagination: {
        current: 1,
        pageSize: 10,
        total: 0,
        showPageSize: true,
        pageSizeOptions: [10, 20, 30, 50],
        showFirstAndLastPageBtn: false,
        onChange: (pageInfo) => {
          Object.assign(this.pagination,pageInfo)
          this.queryParams.pageNum = pageInfo.current || 1;
          this.queryParams.pageSize = pageInfo.pageSize;
          this.getList()
        },
      },
    }
  },
  created() {
    this.queryParams.roleId = this.roleId;
    this.getList();
  },
  methods: {
    getList(){
      this.dataLoading = true;
      unallocatedUserList(this.queryParams).then(response => {
        this.data = response.rows;
        this.pagination.total = response.total;
        this.dataLoading = false;
      }).catch((e) => {
        this.dataLoading = false;
        this.$message.error(e.toString());
      });
    },
    onSubmit() {
      this.getList();
    },
    addSubmit(){
      this.$refs.form.validate().then((validateResult) => {
        if (validateResult === true) {
          authUserSelectAll({ roleId: this.queryParams.roleId, userIds: this.userIds.join(",") }).then((response) => {
            this.$message.success("添加成功");
            this.$emit("reload");
            this.$emit("update:visible", false);
          }).catch((e) => {
            this.$message.error(e.toString());
          });
        }
      });
    },
    closeDialog() {
      this.$emit("update:visible", false);
    },
    rehandleSelectChange(value, { selectedRowData }){
      this.multiple = !value.length;
    }
  }
}
</script>

<style scoped>

</style>
